/**
 * 
 */
package com.iwayin.core.utils;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * 
 */
public class GetPercent {

	public static void main(String[] args) {
		System.out.println(percent(2, 9));
		System.out.println(percent(3, 9));
		System.out.println(percent(2, 9));
		System.out.println(percent(2, 9));
	}

	/**
	 * 获取百分比
	 * 
	 * @param p1
	 * @param p2
	 * @return
	 */
	public static String percent(double p1, double p2) {
		String str;
		double p3 = p1 / p2;
		NumberFormat nf = NumberFormat.getPercentInstance();
		nf.setMinimumFractionDigits(2);
		str = nf.format(p3);
		return str;
	}

	public static List<String> getAllPerCent(List<Long> list, Long totalCount) {
		List<String> percetStr = new ArrayList<String>();
		double total = 0;
		for (int i = 0; i < list.size(); i++) {
			String perCent = GetPercent.percent(list.get(i), totalCount);
			if (i + 1 == list.size()) {
				if ((100 - total) != list.get(i)) {
					perCent = String.valueOf(100 - total).substring(0, 5) + "%";
				}
			}
			String s = perCent.replace("%", "");
			total += Double.valueOf(s);
			percetStr.add(perCent);
			System.out.println(perCent);
		}
		return percetStr;
	}

}
